package com.greenkode.registration.managedbeans;

import java.util.Date;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import com.greenkode.registration.ejb.eao.RegistrationEAO;

/**
 * @author Umoh
 *
 */
@ManagedBean(name="regBean")
@SessionScoped()
public class RegistrationBean {
	private String username;
	private String password;
	private String confirmpassword;
	private String firstname;
	private String lastname;
	private Date dob;
	private String email;
	private String confirmemail;
	private String street;
	private String suburb;
	private String city;
	private int zip;
	private String province;
	private String country;
	
	private String[] selectedGroups;
	
	@EJB RegistrationEAO service;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getConfirmpassword() {
		return confirmpassword;
	}

	public void setConfirmpassword(String confirmpassword) {
		this.confirmpassword = confirmpassword;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public Date getDob() {
		return dob;
	}

	public void setDob(Date dob) {
		this.dob = dob;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getConfirmemail() {
		return confirmemail;
	}

	public void setConfirmemail(String confirmemail) {
		this.confirmemail = confirmemail;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getSuburb() {
		return suburb;
	}

	public void setSuburb(String suburb) {
		this.suburb = suburb;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public int getZip() {
		return zip;
	}

	public void setZip(int zip) {
		this.zip = zip;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}
	
	public String[] getSelectedGroups() {
		return selectedGroups;
	}

	public void setSelectedGroups(String[] selectedGroups) {
		this.selectedGroups = selectedGroups;
	}
	
	private void addMessage(FacesMessage message){
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	
	public void emailValidator(FacesContext context, UIComponent component, Object value){
		//Get the first email address from the attribute
		UIInput emailComponent = (UIInput) component.getAttributes().get("email");
		
		//Retrieve the String value from the component
		String email = (String) emailComponent.getValue();
		
		//Convert the value parameter to a String.
		String confirmEmail = (String) value;
		
		//Check if the email has an @ character and if the @character is not the last character in the string
		if(email.indexOf("@") < 0 || email.indexOf("@") == email.length()-1){
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid Email Address Please use eg. user@domain", null));
		}else if(!email.equals(confirmEmail)){
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Email Addresses don't match", null));
		}
	}

	public String addUser(){
		
		//If Successfull, return success message.
		if(service.persistUser(this)){
			addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "User Registration Successful!!!", null));
			return "success";
		}
		
		//If Unsuccessful, return failure message
		addMessage(new FacesMessage(FacesMessage.SEVERITY_ERROR, "User Registration Failed!!!", null));
		return "failure";
	}
	
	
}
